//
//  UIButton+extension.swift
//  Weibo(Swift)
//
//  Created by 阳 on 16/7/9.
//  Copyright © 2016年 阳. All rights reserved.
//

import UIKit

extension UIButton {
    
    convenience init(image : String? = nil,
        backgroundImage : String? = nil,
        title : String?,
        titleColor : UIColor = UIColor.darkGrayColor(),
        fontSize : CGFloat = 13,
        target : AnyObject? = nil,
        action : String? = nil,
        event : UIControlEvents = .TouchUpInside){
            
            self.init()
            if let image = image{
                
                self.setImage(UIImage(named: image), forState: .Normal)
                self.setImage(UIImage(named: "\(image)_highlighted"), forState: UIControlState.Highlighted)
            }
            if let backgroundImage = backgroundImage{
                self.setBackgroundImage(UIImage(named:backgroundImage ), forState:.Normal)
                self.setBackgroundImage(UIImage(named: "\(backgroundImage)_highlighted" ), forState:.Highlighted)
            }
            self.setTitle(title, forState: .Normal)
            self.titleLabel?.font = UIFont.systemFontOfSize(fontSize)
            self.setTitleColor(titleColor, forState: .Normal)
            if let target = target,let action = action
            {
                let selector = Selector(action)
                self.addTarget(target, action: selector, forControlEvents: event)
            }
            sizeToFit()
    }
}
